package com.v1.video.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.umeng.analytics.MobclickAgent;
import com.v1.video.Constant;
import com.v1.video.R;
import com.v1.video.domain.Carousel;
import com.v1.video.domain.LoginInfo;
import com.v1.video.engine.NetEngine;
import com.v1.video.engine.RequestEngine;
import com.v1.video.util.Helper;
import com.v1.video.util.Logger;
import com.v1.video.util.TransferUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 广告数据适配器
 * 
 * @author wangfang
 * 
 */
public class CarouselAdapter extends BaseAdapter {

	private final String TAG = "CarouselAdapter";
	private Context mContext;
	private LayoutInflater mLayoutInflater;
	private List<Carousel> mLstData;

	public CarouselAdapter(Context context) {
		this.mContext = context;
		mLayoutInflater = LayoutInflater.from(context);
		mLstData = new ArrayList<Carousel>();
	}

	public List<Carousel> getLstData() {
		return mLstData;
	}

	public void setLstData(List<Carousel> mLst) {
		if (mLst != null && mLst.size() > 0) {
			this.mLstData = mLst;
			this.notifyDataSetChanged();
		}
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if (mLstData.size() > 1)
			return Integer.MAX_VALUE;
		else
			return mLstData.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		Logger.i(TAG, "arg0==" + arg0);
		return mLstData.get(arg0 % mLstData.size());
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int arg0, View convertView, ViewGroup arg2) {
		Holder holder = null;
		if (convertView == null) {
			convertView = mLayoutInflater.inflate(
					R.layout.fragment_news_carousel_item, null);
			holder = new Holder();
			holder.imgView = (ImageView) convertView
					.findViewById(R.id.iv_image);
			holder.title = (TextView) convertView.findViewById(R.id.tv_title);
			convertView.setTag(holder);
		} else {
			holder = (Holder) convertView.getTag();
		}
		final int pos = arg0 % mLstData.size();
		final Carousel info = mLstData.get(pos);
		holder.title.setText(info.getTitle());
		ImageLoader.getInstance().displayImage(info.getPic(), holder.imgView,
				Constant.IMAGE_OPTIONS_FOR_HOME_ADVERTISEMENT);
		holder.imgView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (!Helper.checkConnection(mContext)) {
					Toast.makeText(mContext, R.string.net_no_notclick,
							Toast.LENGTH_SHORT).show();
					return;
				}
				if (info != null && !TextUtils.isEmpty(info.getAt())) {
					MobclickAgent.onEvent(mContext, Constant.HOME_SLIDE_ID,
							"首页新闻焦点-" + info.getTitle());
					if (Constant.VIDEO_TYPE_5.equals(info.getAt())) {
						RequestEngine request = new RequestEngine();
						request.sendADVLogToServer(
								mContext,
								Constant.STATE_ADV_CZBM_1 + "",
								Constant.STATE_ADV_CZSX_2 + "_" + info.getTid(),
								"" + Constant.STATE_ADV_CZJG_3);
						if (pos == 0 || pos == 1) {
							String adid = "";
							if (pos == 0) {
								adid = "8";
							} else {
								adid = "9";
							}
							new NetEngine().AdUpToServer(mContext, info
									.getAdid(), LoginInfo.getInstance()
									.getUserId(), adid, "2", "-1");
						}
					}
					TransferUtils.getInstance().transferActivity(mContext,
							info.getTid(), info.getAt(), info.getTitle(),
							info.getUrl(), info.getPic(),
							info.getCommodityid(), Constant.ADV_SOURCE_CAROUSE,"");
				}
			}
		});

		return convertView;
	}

	private class Holder {
		public ImageView imgView;
		public TextView title;
	}
}
